package com.example.demo.Controller;

import com.example.demo.model.MessageInfo;
import com.example.demo.service.MessageInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/message")
@RestController
public class MessageController {
    //存储留言信息
    //private List<MessageInfo> messageInfos = new ArrayList<>();

    @Autowired
    private MessageInfoService messageInfoService;

    @RequestMapping("/getList")
    public List<MessageInfo> getList(){
        return messageInfoService.querMessage();

       // return messageInfos;

    }

    @RequestMapping(value = "publish", produces = "application/json")
    public String puslish(@RequestBody MessageInfo messageInfo){
        if(StringUtils.hasLength(messageInfo.getFrom())
        && StringUtils.hasLength(messageInfo.getTo())
        && StringUtils.hasLength(messageInfo.getMessage())){
         //   messageInfos.add(messageInfo);
            messageInfoService.insertMessage(messageInfo);
            return "{\"ok\":1}";
        }

        return "{\"ok\":0}";

    }


}
